package com.hq.fxh.No5_User.MyModle.Activity.item;


import android.content.Context;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hq.fxh.CustomerView.switchbutton.SwitchButton;
import com.hq.fxh.No5_User.MyModle.AdBannerInfo;
import com.hq.fxh.No5_User.MyModle.AdStoreAdapter;
import com.hq.fxh.No5_User.MyModle.ClearableEidtText;
import com.hq.fxh.R;
import com.hq.fxh.Utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;


public class StoreAdItem extends LinearLayout {

    private List<View> viewContainter = new ArrayList<>();//视图集合
    private TextView tvAdTypeName;
    private ViewPager viewPager;
    private AdBannerInfo.AdvertDetailBean Advert_detail;
    private ClearableEidtText shopName;
    private ImageView goodsPhone;
    private LinearLayout llgoods;
    private LinearLayout llshop;
    private TextView goods_bto;
    private TextView shop_bto;
    private ClearableEidtText goodsName;
    private ClearableEidtText goodsPrice;
    private ClearableEidtText goodsUrl;
    private ImageView shopImg;
    private ClearableEidtText shopPhone;
    private ClearableEidtText shopWChat;
    private AdBannerInfo adBannerInfo;
    private AdBannerInfo.AdvertDetailBean.ProductInfoBean Product_info;
    private ImageView goodsImg;
    private SwitchButton sbSwitch;
    private TextView tvSwitchInfo;
    private OnClickListener onClickListener;
    private AdBannerInfo.AdvertDetailBean.OwnerInfoBean owner_info;
    private ImageView ibClose;

    public StoreAdItem(Context context) {
        super(context);
        inflate(context, R.layout.item_store_ad, this);

        findView();

        initDate();
        tvAdTypeName.setText("店铺广告");
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        shopImg.setOnClickListener(onClickListener);
        goodsImg.setOnClickListener(onClickListener);
        ibClose.setOnClickListener(onClickListener);
    }

    private void initDate() {
        View view1 = LayoutInflater.from(getContext()).inflate(R.layout.fragment_store_ad_item_goods, null);
        initView1(view1);
        goodsName.setSingleLine(true);
        goodsName.setHorizontallyScrolling(true);

        View view2 = LayoutInflater.from(getContext()).inflate(R.layout.fragment_store_ad_item_shop, null);
        initView2(view2);
        shopName.setSingleLine(true);
        shopName.setHorizontallyScrolling(true);
        viewContainter.add(view1);
        viewContainter.add(view2);
        AdStoreAdapter adStoreAdapter = new AdStoreAdapter(viewContainter);
        viewPager.setAdapter(adStoreAdapter);
        pageSelected();
        llgoods.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                goods_bto.setVisibility(VISIBLE);
                shop_bto.setVisibility(INVISIBLE);
                viewPager.setCurrentItem(0, false);
            }
        });
        llshop.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                shop_bto.setVisibility(VISIBLE);
                goods_bto.setVisibility(INVISIBLE);
                viewPager.setCurrentItem(1, false);
            }
        });
    }

    private void pageSelected() {
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    goods_bto.setVisibility(VISIBLE);
                    shop_bto.setVisibility(INVISIBLE);
                } else {
                    shop_bto.setVisibility(VISIBLE);
                    goods_bto.setVisibility(INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void findView() {
        tvAdTypeName = (TextView) findViewById(R.id.tvAdTypeName);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        llgoods = (LinearLayout) findViewById(R.id.llgoods);
        llshop = (LinearLayout) findViewById(R.id.llshop);
        goods_bto = (TextView) findViewById(R.id.goods_bto);
        shop_bto = (TextView) findViewById(R.id.shop_bto);
        sbSwitch = (SwitchButton) findViewById(R.id.sbSwitch);
        tvSwitchInfo = (TextView) findViewById(R.id.tvSwitchInfo);
        ibClose = (ImageView)findViewById(R.id.ibClose);
    }

    private void initView2(View view2) {
        shopImg = (ImageView) view2.findViewById(R.id.shopImg);
        shopName = (ClearableEidtText) view2.findViewById(R.id.shopName);
        shopPhone = (ClearableEidtText) view2.findViewById(R.id.shopPhone);
        shopWChat = (ClearableEidtText) view2.findViewById(R.id.shopWChat);
    }

    private void initView1(View view1) {
        goodsImg = (ImageView) view1.findViewById(R.id.goodsImg);
        goodsName = (ClearableEidtText) view1.findViewById(R.id.goodsName);
        goodsPrice = (ClearableEidtText) view1.findViewById(R.id.goodsPrice);
        goodsUrl = (ClearableEidtText) view1.findViewById(R.id.goodsUrl);
    }


    public void setAdInfo(final AdBannerInfo info) {
        adBannerInfo = info;
        Advert_detail = info.getAdvert_detail();
        if (Advert_detail != null) {
            Product_info = Advert_detail.getProduct_info();
            if (Product_info != null) {
           //     ImageLoader.getInstance().load(getContext(), goodsImg, Product_info.getPic());

                String pic = Product_info.getPic();
                if (pic == null || pic.equals("")) {

                } else {
                    GlideUtils.loadImageView(getContext(), pic, goodsImg);

                }

                goodsName.setText(Product_info.getName());
                goodsPrice.setText(Product_info.getPrice());
                goodsUrl.setText(Product_info.getHref());
            } else {
                Product_info = new AdBannerInfo.AdvertDetailBean.ProductInfoBean();
                Advert_detail.setProduct_info(Product_info);
            }
            owner_info = Advert_detail.getOwner_info();
            if (owner_info != null) {
             //   ImageLoader.getInstance().load(getContext(), shopImg, owner_info.getPic());

                String pic = owner_info.getPic();
                if(pic!=null&&(!pic.equals(""))){
                    GlideUtils.loadImageView(getContext(), pic,shopImg);

                }

                shopName.setText(owner_info.getName());
                shopPhone.setText(owner_info.getMobile());
                shopWChat.setText(owner_info.getWechat());
            } else {
                owner_info = new AdBannerInfo.AdvertDetailBean.OwnerInfoBean();
                Advert_detail.setOwner_info(owner_info);
            }
        } else if (Advert_detail == null) {
            Advert_detail = new AdBannerInfo.AdvertDetailBean();
            Product_info = new AdBannerInfo.AdvertDetailBean.ProductInfoBean();
            Advert_detail.setProduct_info(Product_info);
            owner_info = new AdBannerInfo.AdvertDetailBean.OwnerInfoBean();
            Advert_detail.setOwner_info(owner_info);
            adBannerInfo.setAdvert_detail(Advert_detail);
        }
        goodsName.addTextChangedListener(new TextWatcher() {
            private int maxLen = 24;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Editable editable = goodsName.getText();
                int len = editable.length();
                if (len > maxLen) {
                    ToastUtils.show(getContext(), "输入字符不能超过"+maxLen+"个");
                    int selEndIndex = Selection.getSelectionEnd(editable);
                    String str = editable.toString();
                    //截取新字符串
                    String newStr = str.substring(0, maxLen);
                    goodsName.setText(newStr);
                    editable = goodsName.getText();

                    //新字符串的长度
                    int newLen = editable.length();
                    //旧光标位置超过字符串长度
                    if (selEndIndex > newLen) {
                        selEndIndex = editable.length();
                    }
                    //设置新光标所在的位置
                    Selection.setSelection(editable, selEndIndex);

                }

            }

            @Override
            public void afterTextChanged(Editable s) {
                Advert_detail.getProduct_info().setName(s.toString());
            }
        });
        goodsPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Advert_detail.getProduct_info().setPrice(s.toString());
            }
        });
        goodsUrl.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString();
                if (str.contains("【")
                        || str.contains("】")
                        || str.contains("，")
                        || str.contains("：")
                        || str.contains("；")) {
                    ToastUtils.show(getContext(), "链接不能包含特殊字符");
                }
                Advert_detail.getProduct_info().setHref(str);
            }
        });
        shopName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Advert_detail.getOwner_info().setName(s.toString());
            }
        });
        shopPhone.addTextChangedListener(new TextWatcher() {
            private int maxLen = 24;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Editable editable = shopPhone.getText();
                int len = editable.length();
                if (len > maxLen) {
                    ToastUtils.show(getContext(), "输入字符不能超过"+maxLen+"个");
                    int selEndIndex = Selection.getSelectionEnd(editable);
                    String str = editable.toString();
                    //截取新字符串
                    String newStr = str.substring(0, maxLen);
                    shopPhone.setText(newStr);
                    editable = shopPhone.getText();

                    //新字符串的长度
                    int newLen = editable.length();
                    //旧光标位置超过字符串长度
                    if (selEndIndex > newLen) {
                        selEndIndex = editable.length();
                    }
                    //设置新光标所在的位置
                    Selection.setSelection(editable, selEndIndex);

                }

            }

            @Override
            public void afterTextChanged(Editable s) {
                Advert_detail.getOwner_info().setMobile(s.toString());
            }
        });
        shopWChat.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Advert_detail.getOwner_info().setWechat(s.toString());
            }
        });

        sbSwitch.setVisibility(View.VISIBLE);
        sbSwitch.setChecked(BooleanUtils.intToBool(info.getIs_carousel()));
        setText(sbSwitch.isChecked());
        sbSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                info.setIs_carousel(BooleanUtils.BoolToInt(isChecked));
            }
        });
    }

    public void setImage(String url, int resId) {
        if (resId == R.id.goodsImg) {
          //  ImageLoader.getInstance().load(getContext(), goodsImg, url);
            GlideUtils.loadImageView(getContext(),url,goodsImg);
            if (adBannerInfo != null && adBannerInfo.getAdvert_detail() != null && adBannerInfo.getAdvert_detail().getProduct_info() != null) {
                adBannerInfo.getAdvert_detail().getProduct_info().setPic(url);
            } else {
                AdBannerInfo.AdvertDetailBean advertDetailBean = new AdBannerInfo.AdvertDetailBean();
                AdBannerInfo.AdvertDetailBean.ProductInfoBean productInfoBean = new AdBannerInfo.AdvertDetailBean.ProductInfoBean();
                productInfoBean.setPic(url);
                advertDetailBean.setProduct_info(productInfoBean);
                adBannerInfo.setAdvert_detail(advertDetailBean);
            }
        } else {
         //   ImageLoader.getInstance().load(getContext(), shopImg, url);
            GlideUtils.loadImageView(getContext(),url,shopImg);

            if (adBannerInfo != null && adBannerInfo.getAdvert_detail() != null && adBannerInfo.getAdvert_detail().getOwner_info() != null) {
                adBannerInfo.getAdvert_detail().getOwner_info().setPic(url);
            } else {
                AdBannerInfo.AdvertDetailBean advertDetailBean = new AdBannerInfo.AdvertDetailBean();
                AdBannerInfo.AdvertDetailBean.OwnerInfoBean ownerInfoBean = new AdBannerInfo.AdvertDetailBean.OwnerInfoBean();
                ownerInfoBean.setPic(url);
                advertDetailBean.setOwner_info(ownerInfoBean);
                adBannerInfo.setAdvert_detail(advertDetailBean);
            }
        }
    }

    public void setText(boolean isCheck) {
        if (isCheck) {
            tvSwitchInfo.setText(R.string.ad_on_tip);

        } else {
            tvSwitchInfo.setText(R.string.ad_off_tip);

        }
    }
}
